//
// Created by rengen.gong on 2021/12/7.
// 自定义一个 String 类
// 这个类实现了各种操作符重载 类似 << ,+ =的操作
//

#ifndef LEARN1_STRING_H
#define LEARN1_STRING_H

#include "iostream"
#include "cstring"

using namespace std;

class String {

private:
    int length;//字符串长度
    char *sPtr;

//    void setString(const char *s2);
    friend ostream &operator<<(ostream &os, const String &s) {
        return os << s.sPtr;
    }

public:
    String(const char * = "");

    const String &operator=(const String &R) {
        length = R.length;
        strcpy(R.sPtr, sPtr);
        return *this;
    }

};

String::String(const char *str) {
    sPtr = new char[strlen(str) + 1];
    strcpy(sPtr, str);
    length = strlen(str);
}


void useString() {
    String s1("happy"), s2("new year"), s3;

    cout
            << "s1 =" << s1 << "\n"
            << "s2 =" << s2 << "\n"
            << "s3 =" << s3 << "\n"
            //            << "\n比较s2和s1=" <<
            << endl;

}

#endif //LEARN1_STRING_H
